﻿/*
 * ********************************************************************* *
  
Copyright HLRS, University Stuttgart

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
* ********************************************************************* *
*/

using System;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;
using System.Reflection;
using System.Diagnostics;
using System.ServiceModel;

namespace GatewayLibrary
{
    public class GatewayDispatchOperationSelector : IDispatchOperationSelector, IEndpointBehavior
    {
        public GatewayDispatchOperationSelector() { }

        #region IDispatchOperationSelector Members

        string IDispatchOperationSelector.SelectOperation(ref Message message)
        {
            string messageMethodName = message.Headers.Action.ToString();

            MethodInfo[] methods = typeof(IGateway).GetMethods();
            Debug.Assert(1 == methods.Length, "1 == methods.Length");

            string name = methods[0].Name;

            return name;
        }

        #endregion

        #region IEndpointBehavior Members

        void IEndpointBehavior.AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            throw new NotSupportedException();
        }

        void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            endpointDispatcher.DispatchRuntime.OperationSelector = this;
        }

        void IEndpointBehavior.Validate(ServiceEndpoint endpoint)
        {
        }

        #endregion
    }
}